import numpy as np
import pandas as pd
from sklearn.model_selection import KFold, cross_val_score
from sklearn.svm import SVR
from sklearn.metrics import make_scorer, mean_squared_error
import matplotlib.pyplot as plt

# 加载数据集
data = pd.read_csv("src\\regression.csv")

# 删除有缺失的样本
data.dropna(inplace=True)

# 提取特征和标签
X = data.iloc[:, :-1]  # 特征
y = data.iloc[:, -1]  # 标签

# 设置不同的C值和epsilon值进行实验
C_values = [0.1, 1.0, 10.0]
epsilon_values = [0.01, 0.1, 1.0]

# 让 k 作为变量，用户可以选择不同的 k 值进行实验
k_values = [3, 5, 10]  # 设置不同的k折交叉验证数，例如 3折, 5折, 10折

# 保存结果的字典
all_results = {}

# 自定义MSE评价指标（负值是因为cross_val_score返回的是负误差）
mse_scorer = make_scorer(mean_squared_error, greater_is_better=False)

# 遍历不同的k值
for k in k_values:
    results = []

    # 设置k折交叉验证
    kf = KFold(n_splits=k, shuffle=True, random_state=42)

    for C in C_values:
        for epsilon in epsilon_values:
            # 使用支持向量机回归（SVR）模型
            model = SVR(kernel='rbf', C=C, epsilon=epsilon)

            # 使用k折交叉验证进行训练和验证
            mse_scores = cross_val_score(model, X, y, cv=kf, scoring=mse_scorer)
            avg_mse = -np.mean(mse_scores)  # 取负值使MSE为正

            # 保存结果
            results.append({
                'C': C,
                'epsilon': epsilon,
                'MSE': avg_mse
            })
            print(f"K: {k}, C: {C}, Epsilon: {epsilon}, Mean Squared Error (MSE): {avg_mse}")

    # 将当前k值的实验结果保存
    all_results[k] = pd.DataFrame(results)

# 结果可视化
plt.figure(figsize=(12, 8))

# 为每个k值画一张图
for k in k_values:
    results_df = all_results[k]
    for C in C_values:
        subset = results_df[results_df['C'] == C]
        plt.plot(subset['epsilon'], subset['MSE'], marker='o', label=f'k={k}, C={C}')

plt.title('SVM Regression MSE vs Epsilon for Different C and k values')
plt.xlabel('Epsilon')
plt.ylabel('Mean Squared Error')
plt.legend()
plt.grid(True)
plt.show()
